﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Math;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;

namespace TerrainGenerator.Physics
{
    class PhysicsObject
    {
        protected Vector3 position;
        protected Vector3 scale;

        protected Body body;
        public Body Body { get { return body; } }
        protected CollisionSkin skin;
        public CollisionSkin CollisionSkin { get { return skin; } }

        public PhysicsObject(Vector3 position, Vector3 scale)
        {
            this.position = position;
            this.scale = scale;
        }

        protected Vector3 SetMass(float mass)
        {
            PrimitiveProperties primitiveProperties = new PrimitiveProperties(
                PrimitiveProperties.MassDistributionEnum.Solid,
                PrimitiveProperties.MassTypeEnum.Mass, mass);

            float junk;
            Vector3 com;
            Matrix it;
            Matrix itCoM;

            skin.GetMassProperties(primitiveProperties, out junk, out com, out it, out itCoM);

            Body.BodyInertia = itCoM;
            Body.Mass = junk;

            return com;
        }

        protected Matrix GetWorldMatrix()
        {
            return
                Matrix.CreateScale(scale) *
                skin.GetPrimitiveLocal(0).Transform.Orientation *
                body.Orientation *
                Matrix.CreateTranslation(body.Position);
        }
    }
}
